package util;

import annotations.ExcelColumn;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/10/5.
 */
public class POIUtil {

    public static void write(List dataList,String targetFile){
        Class<?> clazz = dataList.get(0).getClass();
        List<String> headList= new ArrayList<String>();
        List<Method> getMethod = new ArrayList<Method>();
        try {
            getReflectInfo(clazz,headList,getMethod);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }

        // 先创建工作簿对象
        HSSFWorkbook workbook2003 = new HSSFWorkbook();
        HSSFCellStyle hlink_style = workbook2003.createCellStyle();
        HSSFFont hlink_font = workbook2003.createFont();
        hlink_font.setUnderline(HSSFFont.U_SINGLE);
        hlink_font.setColor(HSSFColor.BLUE.index);
        hlink_style.setFont(hlink_font);
        // 创建工作表对象并命名
        HSSFSheet sheet = workbook2003.createSheet("进馆档案");
        sheet.setDefaultColumnWidth(12);
        sheet.setDefaultRowHeight((short) (2 * 256));
        HSSFRow headRow = sheet.createRow(0);
        for(int m=0;m<headList.size();m++){
            HSSFCell cell = headRow.createCell(m);
            cell.setCellValue(headList.get(m));
        }
        // 遍历集合对象创建行和单元格
        HSSFCell cell;Method method;Object result;
        for (int i = 0; i < dataList.size(); i++) {
            // 取出Student对象
            Object obj = dataList.get(i);
            // 创建行
            HSSFRow row = sheet.createRow(i+1);
            for (int j =0;j<getMethod.size();j++) {
                try {
                    method = getMethod.get(j);
                    ExcelColumn excelColumns = method.getAnnotation(ExcelColumn.class);
                    if(excelColumns != null && excelColumns.link()){
                        Map<String,String> map = (Map<String, String>) method.invoke(obj);
                        cell = row.createCell(j);
                        cell.setCellValue(map.get("value") == null ? "" : map.get("value").toString());
                        HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
                        link.setAddress(map.get("link"));
                        cell.setHyperlink(link);
                        cell.setCellStyle(hlink_style);
                    }else {
                        result = method.invoke(obj);
                        cell = row.createCell(j);
                        cell.setCellValue(result == null ? "" : result.toString());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // 生成文件
        File file = new File(targetFile);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            workbook2003.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static void getReflectInfo(Class<?> c,List<String> head,List<Method> getMethod) throws IntrospectionException {
        Field[] fields = c.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            if (!fields[i].getName().equalsIgnoreCase("serialVersionUID") && !fields[i].getName().equalsIgnoreCase("$jacocoData")) {
                ExcelColumn excelColumns = fields[i].getAnnotation(ExcelColumn.class);
                if (excelColumns != null) {
                    head.add(excelColumns.name());
                } else {
                    head.add(fields[i].getName());
                }
                getMethod.add(new PropertyDescriptor(fields[i].getName(), c).getReadMethod());
            }
        }
    }
}
